<?php
namespace app\index\controller;
use app\common\exception\System as SystemException;
use app\index\model\Smallvideo as model;
use app\index\model\Imgtext as News;
use app\index\model\Comment as Newscomment;
use app\index\model\Videocomment;
use app\index\model\Comment;
use think\Db;
use app\index\model\Newspraise;

class Smallvideo extends Common
{
    /**
   * 不需要登录
   * @var array
   */
  protected $noNeedLogin = ['*'];

  public function details(){
    $id = $this->request->param("id"); 
    $member_id=$this->auth->member_id;
    $video=model::where("id",">=",$id)->field("id,member_id,title,type,url,thumb")->find();
    if($video){
    	$video=$video->toArray();
    	$video['praise']=$this->getPraise($video,$member_id);
    	$video['follow']=$this->getFollow($video,$member_id);
    	$video['begin']=model::order("id asc")->value("id");
    	$video['end']=model::order("id desc")->value("id");
      	$arr=['status'=>1,'data'=>$video,'msg'=>'获取成功'];
      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
    }else{
      $arr=['status'=>0,'msg'=>'获取失败'];
      return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']);      
    }
  }
  public function commentList(){
    $member_id=$this->auth->member_id;
    $where["news_id"]=$this->request->param("news_id");
    $where["type"]=4;
  	$res=Comment::where($where)->field("id,news_id,member_id,parent_id,comment_id,content,create_time")->order("id asc")->select()->toArray();
    

    $where["comment_id"]=0;
    $count=Comment::where($where)->count();
    foreach($res as $key=>$value){
      $whe['news_id']=$value['news_id'];
      $whe['member_id']=$member_id;
      $whe['type']=4;
      $whe['comment_id']=$value['id'];
      $comment_praise=Newspraise::where($whe)->find();

      $how['comment_id']=$value['id'];
      $how['type']=4;
      $how['news_id']=$value['news_id'];
      $res[$key]['praise_count']=Newspraise::where($how)->count();
      if($comment_praise){
        $res[$key]['comment_praise']=1;
      }else{
        $res[$key]['comment_praise']=0;
      }
    }
  	$Comment=$this->getTree($res);
  	if($Comment){
      $arr=['status'=>1,'data'=>$Comment,'count'=>$count,'msg'=>'获取成功'];
      return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
    }else{
      $arr=['status'=>0,'msg'=>'获取失败'];
      return $this->successReturn($res,SystemException::E_SUCCESS, $arr['msg']);      
    }
  }
  public function getTree($data,$pid = 0){
      $tree = [];
      foreach($data as $key=>$value){

          if($value['comment_id'] == $pid){
              $value['child'] = $this->getTree($data,$value['id']);
              $tree[] = $value;
          }
      }
      return $tree;
  }

  public function getPraise($data,$member_id){
    	$wh['member_id']=$member_id;
    	$wh['news_id']=$data['id'];
    	$wh['type']=4;
    	$wh['comment_id']=0;
    	$state=db("news_praise")->where($wh)->count();


    	$where['news_id']=$data['id'];
    	$where['type']=4;
    	$where['comment_id']=0;
    	$count=db("news_praise")->where($where)->count();

    	if($state==0){
    		$arr['state']=0;
    	}else{
    		$arr['state']=1;
    	}
    	$arr['count']=$count;
    	return $arr;
    }
    public function getFollow($data,$member_id){
    	$where['member_id']=$member_id;
    	$where['follow_member_id']=$data['member_id'];
    	$res=db("news_follow")->where($where)->find();
    	if($res){
    		return 1;
    	}else{
    		return 0;
    	}
    }
  public function add()
  {
    $data=$this->request->param();
    $data['member_id']=$this->auth->member_id;
    $model = new model;
    $res=$model->allowField(true)->save($data);
    if($res){
      $arr=['status'=>1,'msg'=>'提交成功'];
      return $this->successReturn($data,SystemException::E_SUCCESS, $arr['msg']); 
    }else{
      $arr=['status'=>0,'msg'=>'提交失败'];
      return $this->successReturn($res,SystemException::E_SUCCESS, $arr['msg']);      
    }
  }
  public function praise(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_praise")->where($data)->find(); 
    if($res){
    	$result=db("news_praise")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消点赞'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_praise")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'点赞成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'点赞失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function Collect(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_collect")->where($data)->find(); 
    if($res){
    	$result=db("news_collect")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消收藏'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_collect")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'收藏成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'收藏失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function comments(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
  	$Comment=new Comment;
  	$res=$Comment->allowField(true)->save($data);
  	if($res){
    		$arr=['status'=>1,'msg'=>'评论成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>0,'msg'=>'评论失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
  }
}
